<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\FileLog;
use app\common\model\Banner;
use app\common\model\BannerCate;
use app\common\model\Naire;
use app\common\model\VisitShow;
use think\facade\View;
use think\facade\Config;
use think\facade\Cache;


class Content extends Base
{
    // 轮播列表
    public function bannerList()
    {
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $limit = isset($get['limit']) && $get['limit'] ? $get['limit'] : 10;
            $page = isset($get['page']) && $get['page'] ? $get['page'] : 1;
            $bannerModel = new Banner();
            $sort = 'status desc,sort ' . Config::get('sysconfig')['sort'];
            $bannerlist = $bannerModel->getPagelist([], '', $sort, $limit, $page);
            // 查询图
            foreach ($bannerlist as &$v) {
                $v['file_path'] = FileLog::getPath($v['file_id']);
                $v['cate_name'] = BannerCate::getCateName($v['cate_id']);
            }
            $count = $bannerModel->countRows([]);
            return json(["code" => 0, "msg" => '成功', "count" => $count, "data" => $bannerlist]);
        } else {
            return View::fetch();
        }
    }
    // 发布轮播
    public function saveBanner()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            $addBanner = $post;
            // 状态
            if (isset($addBanner['status']) && $addBanner['status'] == 'on') {
                $addBanner['status'] = 1;
            } else {
                $addBanner['status'] = 0;
            }
            if(!isset($addBanner['file_id']) || !$addBanner['file_id']){
                return json(['code' => 0, 'msg' => '请上传图片']);
            }
            // 判断修改
            if (isset($addBanner['id']) && $addBanner['id']) {
                try {
                    Banner::update($addBanner);
                    return json(['code' => 1, 'msg' => '更新成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            } else {
                // 新增
                try {
                    $addBanner['add_time'] = time();
                    Banner::create($addBanner);
                    return json(['code' => 1, 'msg' => '新增成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            }
        } else {
            $catelist =   new BannerCate;
            $naire =   new Naire;
            $sort = 'sort ' . Config::get('sysconfig')['sort'];
            $list = $catelist->getAllOrder(['status' => 1], '', $sort)->toArray();
            $nairelist = $naire->getAllOrder(['status' => 1], 'id,title', $sort)->toArray();
            View::assign('cateList', $list);
            View::assign('naireList', $nairelist);
            return View::fetch();
        }
    }
    // 删除轮播
    public function delBanner()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                try {
                    Banner::destroy($post['id']);
                    return json(['code' => 1, 'msg' => '删除成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '删除失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }
    // 修改轮播
    public function editBanner()
    {
        $get  = $this->request->get();
        if (isset($get['id']) && $get['id']) {
            $one =  Banner::find($get['id']);
            $one['file_path'] = FileLog::getPath($one['file_id']);
            $catelist =   new BannerCate;
            $naire =   new Naire;
            $sort = 'sort ' . Config::get('sysconfig')['sort'];
            $list = $catelist->getAllOrder(['status' => 1], '', $sort)->toArray();
            $nairelist = $naire->getAllOrder(['status' => 1], 'id,title', $sort)->toArray();
            View::assign('cateList', $list);
            View::assign('one', $one);
            View::assign('naireList', $nairelist);
            return View::fetch();
        } else {
            return json(['code' => 0, 'msg' => '缺少参数']);
        }
    }
    // 轮播状态
    public function bannerStatus()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                $save = [];
                $save['id'] = $post['id'];
                $save['status'] = $post['status'];
                try {
                    if ($save['status'] == 1) {
                        $str = '打开';
                    } else {
                        $str = '关闭';
                    }
                    Banner::update($save);
                    return json(['code' => 1, 'msg' => $str . '成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => $str . '失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }
    // 轮播分类
    public function bannerCate()
    {
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $limit = isset($get['limit']) && $get['limit'] ? $get['limit'] : 10;
            $page = isset($get['page']) && $get['page'] ? $get['page'] : 1;
            $bannerCateModel = new BannerCate();
            $sort = 'sort ' . Config::get('sysconfig')['sort'];
            $catelist = $bannerCateModel->getPagelist([], '', $sort, $limit, $page);
            $count = $bannerCateModel->countRows([]);
            return json(["code" => 0, "msg" => '成功', "count" => $count, "data" => $catelist]);
        } else {
            return View::fetch();
        }
    }
    // 保存轮播分类
    public function saveBannerCate(){
        if (request()->IsAjax()) {
            $post = $this->request->post();
            $addBanner = $post;
            // 状态
            if (isset($addBanner['status']) && $addBanner['status'] == 'on') {
                $addBanner['status'] = 1;
            } else {
                $addBanner['status'] = 1;
            }
            // 判断修改
            if (isset($addBanner['id']) && $addBanner['id']) {
                try {
                    BannerCate::update($addBanner);
                    return json(['code' => 1, 'msg' => '更新成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            } else {
                // 新增
                try {
                    $addBanner['add_time'] = time();
                    BannerCate::create($addBanner);
                    return json(['code' => 1, 'msg' => '新增成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            }
        } else {
            return View::fetch();
        }
    }
    // 删除轮播分类
    public function delBannerCate(){
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                try {
                    BannerCate::destroy($post['id']);
                    return json(['code' => 1, 'msg' => '删除成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '删除失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }
    // 修改轮播分类
    public function editBannerCate(){
        $get  = $this->request->get();
        if (isset($get['id']) && $get['id']) {
            $one =  BannerCate::find($get['id']);
            View::assign('one', $one);
            return View::fetch();
        } else {
            return json(['code' => 0, 'msg' => '缺少参数']);
        }
    }
    // 预约参观
    public function visitShow(){
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $limit = isset($get['limit']) && $get['limit'] ? $get['limit'] : 10;
            $page = isset($get['page']) && $get['page'] ? $get['page'] : 1;
            $VisitShow = new VisitShow();
            $sort = 'status desc,sort ' . Config::get('sysconfig')['sort'];
            $visitlist = $VisitShow->getPagelist([], '', $sort, $limit, $page);
            // 查询图
            foreach ($visitlist as &$v) {
                $v['file_path'] = FileLog::getPath($v['file_id']);
            }
            $count = $VisitShow->countRows([]);
            return json(["code" => 0, "msg" => '成功', "count" => $count, "data" => $visitlist]);
        } else {
            return View::fetch();
        }
    }
    // 保存预约参观
    public function saveVisitShow(){
        if (request()->IsAjax()) {
            $post = $this->request->post();
            $addShow = $post;
            // 状态
            if (isset($addShow['status']) && $addShow['status'] == 'on') {
                $addShow['status'] = 1;
            } else {
                $addShow['status'] = 0;
            }
            $addressarr = explode(',', $addShow['lnginfo']);
            if (!$addressarr[0] || !$addressarr[1]) {
                return json(['code' => 0, 'msg' => '获取经纬度失败']);
            }
            $gcj02_arr = bd09_To_Gcj02($addressarr[0], $addressarr[1]);
            $addShow['longitude'] = $gcj02_arr[0];
            $addShow['latitude'] = $gcj02_arr[1];
            // 判断修改
            if (isset($addShow['id']) && $addShow['id']) {
                try {
                    VisitShow::update($addShow);
                    return json(['code' => 1, 'msg' => '更新成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            } else {
                // 新增
                try {
                    $addShow['add_time'] = time();
                    VisitShow::create($addShow);
                    return json(['code' => 1, 'msg' => '新增成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            }
        } else {
            return View::fetch();
        }
    }
    // 修改信息
    public function editVisitShow(){
        $get  = $this->request->get();
        if (isset($get['id']) && $get['id']) {
            $one =  VisitShow::find($get['id']);
            $bd09 = gcj02_To_Bd09($one['longitude'], $one['latitude']);
            $one['lnginfo'] = $bd09[0] . ',' . $bd09[1];
            $one['file_path'] = FileLog::getPath($one['file_id']);
            View::assign('one', $one);
            return View::fetch();
        } else {
            return json(['code' => 0, 'msg' => '缺少参数']);
        }
    }
    // 删除信息
    public function delVisitShow(){
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                try {
                    VisitShow::destroy($post['id']);
                    return json(['code' => 1, 'msg' => '删除成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '删除失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }
    // 信息状态
    public function visitStatus()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                $save = [];
                $save['id'] = $post['id'];
                $save['status'] = $post['status'];
                try {
                    if ($save['status'] == 1) {
                        $str = '打开';
                    } else {
                        $str = '关闭';
                    }
                    VisitShow::update($save);
                    return json(['code' => 1, 'msg' => $str . '成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => $str . '失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }
}
